//
//  DataBaseHaneler.m
//  shise
//
//  Created by 徐国庆 on 14/12/21.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "DataBaseHaneler.h"

@implementation DataBaseHaneler

+ (DataBaseHaneler *)shareInstance
{
    static DataBaseHaneler *handler = nil;
    if (handler == nil) {
        
        handler = [[DataBaseHaneler alloc] init];
    }
    return handler;
}

// 打开数据库
- (BOOL)openDB
{
    // 拼接文件路径
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [string stringByAppendingPathComponent:@"food.db"];
    NSLog(@"数据库地址:%@", dbPath);
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    
    return [self judgeResult:result string:@"打开"];
    
}

// 创建美食表
- (BOOL)createFoodTable
{
//    NSString *stirng = [NSString stringWithFormat:@"food"];
//    NSString *sql = [NSString stringWithFormat:@"create table %@(type text, objId text, pic_url text primary key, likeCount text, userId text, userName text, content text, location text, desc text, clickPraiseCount int, userHeadeImageViewAddress text)", stirng];
//    NSLog(@"创建美食表%@", sql);
    NSString *sql = @"create table food(type text, objId text, pic_url text primary key, likeCount text, userId text, userName text, content text, location text, desc text, clickPraiseCount int, userHeadeImageViewAddress text, currentUserName text)";

    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result string:@"创建美食表"];

}

// 添加美食数据
- (BOOL)insertFood:(Food *)food userName:(NSString *)currentUserName
{
    // 添加美食数据之前需要对用户是否登录的状态进行判断 如果已经登录将用户名作为数据的一条属性进行存储
    NSString *sql = [NSString stringWithFormat:@"insert into food values('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%ld', '%@', '%@')", food.type, food.objId, food.pic_url, food.likeCount, food.user, food.userName, food.content, food.location, food.desc, food.clickPraiseCount, food.userHeadeImageViewAddress, currentUserName];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result string:@"添加美食数据"];
}

// 删除美食数据
- (BOOL)deleteFoodWithPic_url:(NSString *)pic_url
{

    NSString *sql = [NSString stringWithFormat:@"delete from food where pic_url = '%@'", pic_url];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result string:@"删除数据"];
}

// 查询美食数据
- (NSMutableArray *)selectAll
{
    // 创建数据库替身 存储对数据库的所有操作
    sqlite3_stmt *stmt = nil;
    // 准备sql语句并执行
//    NSString *sql = [NSString stringWithFormat:@"select * from food", na];
    NSString *sql = @"select * from food";
    int result = sqlite3_prepare(dbPoint, [sql UTF8String], -1, &stmt, NULL);
    // 创建一个可变数组用来存储数据
    NSMutableArray *foodArr = [NSMutableArray array];
    // 遍历所有数创建相应的美食对象
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            Food *food = [[Food alloc] init];
            const unsigned char *typeChar = sqlite3_column_text(stmt, 0);
            // 将char*字符串转为nsstring字符串
            NSString *type = [NSString stringWithUTF8String:(const char *)typeChar];
            
            const unsigned char *objIdChar = sqlite3_column_text(stmt, 1);
            // 将char*字符串转为nsstring字符串
            NSString *objId = [NSString stringWithUTF8String:(const char *)objIdChar];

            const unsigned char *pic_urlChar = sqlite3_column_text(stmt, 2);
            // 将char*字符串转为nsstring字符串
            NSString *pic_url = [NSString stringWithUTF8String:(const char *)pic_urlChar];

            const unsigned char *likeCountChar = sqlite3_column_text(stmt, 3);
            // 将char*字符串转为nsstring字符串
            NSString *likeCount = [NSString stringWithUTF8String:(const char *)likeCountChar];

            const unsigned char *userIdChar = sqlite3_column_text(stmt, 4);
            // 将char*字符串转为nsstring字符串
            NSString *userId = [NSString stringWithUTF8String:(const char *)userIdChar];

            const unsigned char *userNameChar = sqlite3_column_text(stmt, 5);
            // 将char*字符串转为nsstring字符串
            NSString *userName = [NSString stringWithUTF8String:(const char *)userNameChar];

            const unsigned char *contentChar = sqlite3_column_text(stmt, 6);
            // 将char*字符串转为nsstring字符串
            NSString *content = [NSString stringWithUTF8String:(const char *)contentChar];

            const unsigned char *locationChar = sqlite3_column_text(stmt, 7);
            // 将char*字符串转为nsstring字符串
            NSString *location = [NSString stringWithUTF8String:(const char *)locationChar];

            const unsigned char *descChar = sqlite3_column_text(stmt, 8);
            // 将char*字符串转为nsstring字符串
            NSString *desc = [NSString stringWithUTF8String:(const char *)descChar];
            
            int clickPraiseCount = sqlite3_column_int(stmt, 9);
            
            const unsigned char *userHeadeImageViewAddressChar = sqlite3_column_text(stmt, 10);
            NSString *userHeadeImageViewAddress = [NSString stringWithUTF8String:(const char *)userHeadeImageViewAddressChar];
            
            const unsigned char *currentUserNameChar = sqlite3_column_text(stmt, 11);
            NSString *currentUserName = [NSString stringWithUTF8String:(const char *)currentUserNameChar];
//            const unsigned char *clickPraiseCountChar = sqlite3_column_text(stmt, 9);
//            NSString *clickPraiseCount = [NSString stringWithUTF8String:(const char *)clickPraiseCountChar];
//            int clickPraiseCount = sqlite3_column_text(stmt, 9);
//            NSDictionary *dic = sqlite3_column_text(stmt, 9);
            
            food.type = type;
            food.objId = objId;
            food.pic_url = pic_url;
            food.likeCount = likeCount;
            food.userId = userId;
            food.userName = userName;
            food.content = content;
            food.location = location;
            food.desc = desc;
            food.clickPraiseCount = clickPraiseCount;
            food.userHeadeImageViewAddress = userHeadeImageViewAddress;
            food.currentUserName = currentUserName;
//            food.user = dic;
            
            [foodArr addObject:food];
            [food release];
            
        }
        
    }
    sqlite3_finalize(stmt);
    return foodArr;
    
}

- (BOOL)judgeResult:(int)result string:(NSString *)text
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", text);
        return YES;
    }else {
        
        NSLog(@"%@失败", text);
        NSLog(@"错误信息:%d", result);
        return NO;
    }
}

@end
